﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace TST.SharePointObjects
{
    internal class ContentTypeListReferences : ObjectReferenceList
    {
        private List<string> _demoItems;
        private List<IndexDemoItem> _indexDemoItems;

        public ContentTypeListReferences(LicensingMode licensingMode) :
            base(licensingMode)
        {
            _demoItems = new List<string>();
            if (LicensingMode == LicensingMode.Demo)
            {
                _demoItems = Globals.GetDemoContentTypes();
                _indexDemoItems = new List<IndexDemoItem>();
            }
        }

        public override ObjectReferenceType ObjectReferenceType
        {
            get { return ObjectReferenceType.ContentTypeList; }
        }

        internal void Add(SPList list, SiteObjectList objects, SPWeb web, SPSite site, SPWebApplication webApplication)
        {
            foreach (SPContentType cType in list.ContentTypes)
            {
                SiteObject siteObject = objects.GetItemById(cType.Parent.Id.ToString());
                if (siteObject != null && siteObject.Index)
                {
                    if (Globals.MatchingScope(siteObject, webApplication, site, web))
                    {
                        string refId = Globals.GetReferenceId(siteObject, cType.Parent);
                        if (LicensingMode == LicensingMode.FullLicense ||
                            (LicensingMode == LicensingMode.Demo &&
                            _demoItems.Contains(cType.Parent.Id.ToString().ToLower()) &&
                            CountItem(cType.Parent.Id) <= 5))
                        {
                            ObjectReference newReference = new ObjectReference(list.ID.ToString("B"), list.Title, refId, ObjectReferenceType.ContentTypeList);
                            newReference.Url = string.Format("{0}/_layouts/ManageContentType.aspx?List={1}&ctype={2}", web.Url, System.Web.HttpUtility.UrlEncode(list.ID.ToString()), cType.Id.ToString());
                            newReference.SiteTitle = web.Title;
                            newReference.SiteUrl = web.Url;
                            newReference.ListTitle = list.Title;
                            newReference.ListUrl = site.MakeFullUrl(list.DefaultViewUrl);
                            Add(newReference);
                            if (siteObject.IndexItems)
                            {
                                AddItemReferences(cType, siteObject, list, site, web, refId);
                            }
                        }
                    }
                }
            }
        }

        private void AddItemReferences(SPContentType cType, SiteObject siteObject, SPList list, SPSite site, SPWeb web, string refId)
        {
            foreach (SPListItem item in list.Items)
            {
                if (item.ContentType!=null && item.ContentType.Parent.Id==cType.Parent.Id)
                {
                    ObjectReference newReference = new ObjectReference(item.UniqueId.ToString("B"), Globals.GetItemTitle(item), refId, ObjectReferenceType.ContentTypeListItem);
                    newReference.Url = Globals.GetItemUrl(site.MakeFullUrl(list.DefaultViewUrl), item, site.MakeFullUrl(item.Url));
                    newReference.SiteTitle = web.Title;
                    newReference.SiteUrl = web.Url;
                    newReference.ListTitle = list.Title;
                    newReference.ListUrl = site.MakeFullUrl(list.DefaultViewUrl);
                    Add(newReference);
                }
            }
        }

        private int CountItem(SPContentTypeId id)
        {
            foreach (IndexDemoItem item in _indexDemoItems)
            {
                if (string.Compare(id.ToString(), item.Id, true) == 0)
                {
                    item.Count++;
                    return item.Count;
                }
            }
            _indexDemoItems.Add(new IndexDemoItem(id.ToString(), 1));
            return 1;
        }

        internal void Add(SPContentType contentType, SiteObjectList objects, SPWeb web, SPSite site, SPWebApplication webApplication)
        {
            // Find parent and check if it is indexed.
            SPContentType parent = contentType.Parent;
            SiteObject siteObject = objects.GetItemById(parent.Id.ToString());
            if (siteObject != null && siteObject.Index)
            {
                if (Globals.MatchingScope(siteObject, webApplication, site, web))
                {
                    string refId = Globals.GetReferenceId(siteObject, parent);
                    if (LicensingMode == LicensingMode.FullLicense ||
                        (LicensingMode == LicensingMode.Demo &&
                        _demoItems.Contains(parent.Id.ToString().ToLower()) &&
                        CountItem(parent.Id) <= 5))
                    {
                        ObjectReference newReference = new ObjectReference(contentType.Id.ToString(), contentType.Name, refId, ObjectReferenceType.ContentTypeParent);
                        newReference.Url = string.Format("{0}/_layouts/ManageContentType.aspx?ctype={1}", web.Url, contentType.Id.ToString());
                        newReference.SiteTitle = web.Title;
                        newReference.SiteUrl = web.Url;
                        newReference.ListTitle = string.Empty;
                        newReference.ListUrl = string.Empty;
                        Add(newReference);
                    }
                }
            }
        }
    }
}
